package org.alex.samples.service;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.alex.samples.dao.IUserDAO;
import org.alex.samples.model.User;
import org.alex.samples.vo.UserVO;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import util.ExcelUtils;

@Service
public class UserServiceImpl implements IUserService {
	@Autowired
	private IUserDAO userDAO;
	
	@Override
	public List<UserVO> findAllUsers() throws Exception{
		List<User> list = userDAO.findAllUsers();
		List<UserVO> resultList = new ArrayList<>();
		for(User user:list){
			UserVO uservo = new UserVO();
			uservo.setAddress(user.getAddress());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			uservo.setBirthday(sdf.format(user.getBirthday()));
			uservo.setEmail(user.getEmail());
			uservo.setGender(user.getGender());
			uservo.setId(user.getId());
			uservo.setLast_lock_time(user.getLast_lock_time());
			uservo.setLogin_fail_count(user.getLogin_fail_count());
			uservo.setName(user.getName());
			uservo.setPassword(user.getPassword());
			uservo.setTel(user.getTel());
			resultList.add(uservo);
			}
		return resultList;
	}

	@Override
	public User getUserById(int id) throws Exception {
		return userDAO.getUserById(id);
	}

	@Override
	public void editUser(User user) throws Exception {
		userDAO.editUser(user);
	}

	@Override
	public void createUser(User user) throws Exception {
		userDAO.createUser(user);
	}

	@Override
	public void deleleUser(int id) throws Exception {
		userDAO.deleleUser(id);
	}

	@Override
	public void saveStudentByExcel(InputStream is, String postfix) throws Exception {
		Workbook wk = ExcelUtils.openWorkbook(is,postfix);
		List<User> allExcelUser = ExcelUtils.getStudentListByExcel(wk);
		userDAO.save(allExcelUser);
	}

	@Override
	public User getUserByName(String name) throws Exception {
		return userDAO.getUserByName(name);
	}


}
